/**
 * 点选控件，实现鼠标点击查询
 * @class OpenLayers.Control.PointQuery
 * 继承自: - <OpenLayers.Control>
 * @requires OpenLayers/Control.js
 */
OpenLayers.Control.PointQuery = OpenLayers.Class(OpenLayers.Control,{
	/**
	 * 工具类型
	 * @property type
	 * @type {String}
	 */
	type : OpenLayers.Control.TYPE_TOOL,

	/**
	 * styleMap
	 * @property styleMap
	 * @type {OpenLayers.styleMap}
	 */
	styleMap : null,

	/**
	 * 点选容限
	 * @property tolerance
	 * @type {Number}
	 */
	tolerance : 20,

	/**
	 * 空间查询构建的多边形
	 * @property queryFeature
	 * @type {Number}
	 */
	queryFeature : 2,

	/**
	 * 工具按钮提示文字
	 * @property title
	 * @type {String}
	 */
	title : "点查",
	
	/**
	 * 初始化
	 * @method initialize
	 * @param {Object} options
	 */
	initialize : function(options) {
		OpenLayers.Control.prototype.initialize.apply(this,arguments);
	},

	/**
	 * 控件被触发时设置鼠标手势
	 * @method activate
	 */
	activate : function() {
		OpenLayers.Control.prototype.activate.apply(this,arguments);
		this.map.layerContainerDiv.style.cursor = "url("
				+ OpenLayers.Util.getRootPath()
				+ "img/PointQuery.cur),default";	},
	
	/**
	 * 控件禁用
	 * @method deactivate
	 */
	deactivate : function() {
		OpenLayers.Control.prototype.deactivate.apply(this,arguments);
		this.map.layerContainerDiv.style.cursor = this.map.defaultCursor;
	},

	/**
	 * @method draw
	 */
	draw : function() {
		this.handler = new OpenLayers.Handler.Click(this, {
			click : this.executeQuery
		}, {
			keyMask : this.keyMask,
			layerOptions : {
				styleMap : this.styleMap
			}
		});
	},

	/**
	 * 响应点击事件
	 * @method executeQuery
	 * @param {{<OpenLayers.Event>} evt
	 */
	executeQuery : function(evt) {
		var geometry = this.evtToCoord(evt);
		this.event = evt;
		if(geometry){
			this.spatialQuery(geometry);
		}
	},
	
	/**
	 * evt转为空间对象geometry 
	 */
	evtToCoord : function(evt) {
		var mapResolution = this.map.getResolution();
		var lonlat = this.map.getLonLatFromViewPortPx(evt.xy);
		// 构造点查询小矩形
		var top = lonlat.lat + mapResolution * this.tolerance / 2;
		var right = lonlat.lon + mapResolution * this.tolerance / 2;
		var bottom = lonlat.lat - mapResolution * this.tolerance / 2;
		var left = lonlat.lon - mapResolution * this.tolerance / 2;

		var geometrystr = left+","+ bottom + "," + right + "," + top;
		// 将小矩形坐标构造成geometry对象
		return geometrystr;
	},

	/**
	 * Method: spatialQuery 构造查询条件并提交
	 * 
	 * Parameters: evt
	 */
	spatialQuery : function(geometry) {
		// 查询之前事件回调
		var flag = this.map.events.triggerEvent("preQuery", {
			"queryControl" : this
		});
		// 如果有回调函数并且回调函数返回了false则不运行默认的查询
		if (flag != undefined && !flag){
			return;
		}
		
		this.queryend(geometry);
        
		// 查询之后事件回调
		this.map.events.triggerEvent("postQuery", {
			"queryControl" : this
		});
	},
	
	/**
	 * 
	 */
	queryend: function(geometry){
		
	},
	
	CLASS_NAME : "OpenLayers.Control.PointQuery"
});